﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LabNet.Server.Domain;

namespace LabNet.Server.Data
{
    public abstract class EntityService<T> : IEntityService<T> where T : BaseObject
    {
        protected IEntityRepository<T> Repository;
        protected IUnitOfWork UnitOfWork;

        protected EntityService(IUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
        }

        public virtual IQueryable<T> Get()
        {
            var result = Repository.Get();
            return result;
        }

        public virtual IList<T> GetList()
        {
            var result = Repository.GetList();
            return result;
        }

        public virtual async Task<IList<T>> GetAsync()
        {
            var result = await Repository.GetAsync();
            return result;
        }

        public virtual T GetById(long id)
        {
            var result = Repository.GetById(id);
            return result;
        }

        public virtual T GetById(long? id)
        {
            var result = Repository.GetById(id);
            return result;
        }

        public virtual async Task<T> GetByIdAsync(long id)
        {
            var result = await Repository.GetByIdAsync(id);
            return result;
        }

        public virtual async Task<T> GetByIdAsync(long? id)
        {
            var result = await Repository.GetByIdAsync(id);
            return result;
        }

        public virtual void InsertOrUpdate(T entity)
        {
           Repository.InsertOrUpdate(entity);
        }

        public virtual async Task InsertOrUpdateAsync(T entity)
        {
           InsertOrUpdate(entity);
            await UnitOfWork.CommitAsync();
        }

        public virtual void Add(T entity)
        {
             Repository.Add(entity);
        }

        public virtual void Update(T entity)
        {
            Repository.Update(entity);
        }

        public virtual void Remove(T entity)
        {
           Repository.Remove(entity);
        }

        public virtual int Commi()
        {
            return UnitOfWork.Commit();
        }

        public virtual async Task<int> CommitAsync()
        {
            return await UnitOfWork.CommitAsync();
        }
    }
}
